package com.qf.controller;

import com.qf.constant.JwtClaimsConstant;
import com.qf.mapper.UserMapper;
import com.qf.pojo.dto.PageUserQueryDTO;
import com.qf.pojo.dto.UserDTO;
import com.qf.pojo.dto.UserLoginDTO;
import com.qf.pojo.entity.User;
import com.qf.pojo.vo.UserLoginVO;
import com.qf.pojo.vo.UserVO;
import com.qf.result.PageResult;
import com.qf.result.Result;
import com.qf.service.UserService;
import com.qf.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import com.qf.properties.JwtProperties;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户相关接口
 */
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
@CrossOrigin("http://localhost:8082")
@Slf4j
@Api(tags = "用户相关接口")
public class UserController {

    private final UserService userService;
    private final JwtProperties jwtProperties;


    /**
     * 登录
     */
    @PostMapping("/login")
    @ApiOperation("用户登录")
    public Result login(@RequestBody UserLoginDTO userLoginDTO) {
        log.info("登录参数：{}", userLoginDTO);

        User user = userService.login(userLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID, user.getId());
        String token = JwtUtil.createJWT(
            jwtProperties.getSecretKey(),
            jwtProperties.getTtl(), claims);


        UserLoginVO userLoginVO = UserLoginVO.builder()
            .id(user.getId())
            .userName(user.getUsername())
            .token(token)
            .build();

        return Result.success(userLoginVO);
    }


    /**
     * 注册
     */
    @PostMapping("/register")
    @ApiOperation("用户注册")
    public Result register(@RequestBody UserDTO userDTO) {
        log.info("注册参数：{}", userDTO);
        userService.register(userDTO);
        return Result.success();
    }


    /**
     * 根据id查询用户信息
     */
    @GetMapping("/id/{id}")
    @ApiOperation("根据id查询用户信息")
    public Result<UserVO> getUserById(@PathVariable Long id) {
        log.info("根据id查询用户信息，id为：{}", id);
        UserVO user = userService.getUserById(id);
        return Result.success(user);
    }


    /**
     * 分页查询用户列表
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("分页查询用户列表")
    public Result<PageResult> getPage(PageUserQueryDTO queryDTO) {
        // select * from employee limit 0,10
        log.info("员工分页查询，参数为：{}", queryDTO);
        PageResult pageResult = userService.pageQuery(queryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据id删除用户
     */
    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除用户")
    public Result deleteUser(@PathVariable Long id) {
        userService.deleteUser(id);
        return Result.success();
    }

    /**
     * 批量删除用户
     */
    @DeleteMapping("/batch")
    @ApiOperation("批量删除用户")
    public Result deleteUsers(@RequestBody List<Long> ids) {
        userService.deleteUsers(ids);
        return Result.success();
    }


    /**
     * 设置帐号状态
     */
    @PostMapping("/status/{status}")
    @ApiOperation("设置帐号状态")
    public Result updateUserStatus(@PathVariable Integer status,@RequestBody Map<String, Long> requestBody) {
        Long id = requestBody.get("id");
        log.info("设置账号状态,状态参数:{}, 用户id:{}" , status, id);
        userService.UpdateUserStatus(status, id);
        return Result.success();
    }

    /**
     * 添加用户
     */
    @PostMapping("/add")
    @ApiOperation("添加用户")
    public Result addUser(@RequestBody UserDTO userDTO) {
        userService.register(userDTO);
        return Result.success();
    }

    /**
     * 更新用户
     */
    @PostMapping("/edit")
    @ApiOperation("更新用户")
    public Result editUser(@RequestBody UserDTO userDTO) {
        userService.updateById(userDTO);
        return Result.success();
    }

}